﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Agents.Data;

namespace Agents.Agent
{
    //this page spilles all clients from database on to the map.
    //this allows the agent to see which clients are close to him.
    //the agent will show up on the map as a red circle.
    //the clients will apear as green circles the shading changes based on priority.
    //most of the code for the map is on the client side.
    public partial class CustomersOnMap : System.Web.UI.Page
    {
        //declaring arrays for holding the:
        //locations,names, telphone numbers, date of last order, 
        //priority, x map coordinates, y mao coordinants of all clients in database.
        public string[] Locations = { "1" };
        public string[] names = { "1" };
        public string[] telephones = { "1" };
        public string[] dates = { "1" };
        public string[] priority = { "1" };
        public double[] xCords = { 30.21 };
        public double[] yCords = { 30.21 };
        //this variable will hold the amount of clients in databas (aka- array size)
        public int size = 1;
        //variable for holding the agents address.
        public string agentAt = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //creating the arrays
            Locations = new string[1];
            names = new string[1];
            telephones = new string[1];
            dates = new string[1];
            priority = new string[1];
            xCords = new double[1];
            yCords = new double[1];
            int i = 1;
            size = 0;

            //retrieving the adrress of the agent from previouse page.
            agentAt = Request.QueryString["address"];

            //creating an  instant of the database model container.
            Model1Container contex = new Model1Container();

            //collecting into results all existing clients from database
            var result =
                (from c in contex.Clients
                    select c);
            //for every client we in results we will add that clients information into the arrays.
            foreach (Clients c in result)
            {
                //resising the arrays to thier previous size pluss 1
                System.Array.Resize<string>(ref Locations, i);
                System.Array.Resize<string>(ref names, i);
                System.Array.Resize<string>(ref dates, i);
                System.Array.Resize<string>(ref telephones, i);
                System.Array.Resize<string>(ref priority, i);
                System.Array.Resize<double>(ref xCords, i);
                System.Array.Resize<double>(ref yCords, i);
                //adding this clients values to the arrays.

                Locations[i - 1] = c.GetAddressInESRIFormat();

                xCords[i - 1] = c.x;
                yCords[i - 1] = c.y;
                priority[i - 1] = c.Priority;
                telephones[i - 1] = c.Phone;
                dates[i - 1] = c.LastOrder.ToString();

                // Escape characters on names
                names[i - 1] = c.Name.Replace("'", "");
                int num = i - 1;

                //advansing the counters
                i++;
                size++;
            }

            // Try and get location based notifications
            if (IsPostBack)
            {
                Queries queries = new Queries();

                // Get the agent's location if needed location based notifications
                if (!queries.GetConfiguration().AllowLocationBasedNotifications)
                {
                    updatedAgentLocation.Value = "true";
                }
                else if ((!String.IsNullOrEmpty(hiddenLocationX.Value)) &&
                    (!String.IsNullOrEmpty(hiddenLocationY.Value)) &&
                    (updatedAgentLocation.Value == "false"))
                {
                    double x = Double.Parse(hiddenLocationX.Value);
                    double y = Double.Parse(hiddenLocationY.Value);

                    List<LocationBasedNotifications> notifications = 
                        queries.GetLocationBasedNotifications(x, y);

                    updatedAgentLocation.Value = "true";

                    // Show the popup of notifications
                    if (notifications.Count > 0)
                    {
                        String notificationsIds =
                            String.Join(",", notifications.Select((n) => n.Id));

                        String script = String.Format(
                            "<script type=text/javascript>" +
                            "PopupCenter('NotificationDetails.aspx?notificationsIds={0}'," +
                            "'מערכת סוכנים',630, 330, false);</script>", notificationsIds);

                        ClientScript.RegisterStartupScript(this.GetType(), "onload", script);
                    }
                }
            }
        }

        protected void backBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("Map.aspx");
        }
    }
}